package main

import (
	"fmt"
	"log"
	"os"
	"runtime"
	"runtime/trace"
	"sync"
)

var wg sync.WaitGroup

func runTask(id int) {
	defer wg.Done()
	log.Printf("task %d start...\n", id)
	//time.Sleep(2 * time.Second)
	log.Printf("task %d complete...\n", id)
}

func asyncRun() {
	for i := 0; i < 100; i++ {
		go runTask(i + 1)
		wg.Add(1)
	}
}

func main() {
	f, err := os.Create("./settrace.out")

	if err != nil {
		panic(err)
	}
	defer f.Close()

	err = trace.Start(f)
	if err != nil {
		panic(err)
	}
	defer trace.Stop()
	fmt.Println(runtime.GOMAXPROCS(12))
	asyncRun()
	wg.Wait() //等待全部协程完成工作
}
